package com.lcc.lynx.jdbc.qw;

import com.lcc.lynx.jdbc.enums.Operator;
import com.lcc.lynx.jdbc.enums.QwOperator;

import java.io.Serializable;
import java.util.List;

/**
 * @author lcc
 * @date 2025/7/23 13:46
 */
public class Column<T> extends QwCondition implements Serializable {

    private boolean isDate;
    private String columnAs;

    public Column() {

    }

    public Column(String columnName) {
        this.columnName = columnName;
    }

    public Column(TableDef tableDef, String columnName) {
        this.columnName = columnName;
        this.tableDef = tableDef;
    }

    public Column(TableDef tableDef, String columnName, boolean isDate) {
        this.columnName = columnName;
        this.tableDef = tableDef;
        this.isDate = isDate;
    }

    /**
     * 等于 =
     */
    public QueryCondition eq(T value) {
        return new QueryCondition(columnName, isDate ? Operator.DATE_EQUAL : Operator.EQUAL, tableDef, value);
    }

    /**
     * 不等于 !=
     */
    public QueryCondition ne(T value) {
        return new QueryCondition(columnName, isDate ? Operator.DATE_NOT_EQUAL : Operator.NOT_EQUAL, tableDef, value);
    }

    /**
     * 大于 >
     */
    public QueryCondition gt(T value) {
        return new QueryCondition(columnName, isDate ? Operator.DATE_GREATER_THAN : Operator.GREATER_THAN, tableDef, value);
    }

    /**
     * 大于等于 >=
     */
    public QueryCondition ge(T value) {
        return new QueryCondition(columnName, isDate ? Operator.DATE_GREATER_THAN_OR_EQUAL : Operator.GREATER_THAN_OR_EQUAL, tableDef, value);
    }

    /**
     * 小于 <
     */
    public QueryCondition lt(T value) {
        return new QueryCondition(columnName, isDate ? Operator.DATE_LESS_THAN : Operator.LESS_THAN, tableDef, value);
    }

    /**
     * 小于等于 <=
     */
    public QueryCondition le(T value) {
        return new QueryCondition(columnName, isDate ? Operator.DATE_LESS_THAN_OR_EQUAL : Operator.LESS_THAN_OR_EQUAL, tableDef, value);
    }

    /**
     * 全模糊查询 like %?%
     */
    public QueryCondition like(T value) {
        return new QueryCondition(columnName, Operator.LIKE, tableDef, value);
    }

    /**
     * 非全模糊查询 not like %?%
     */
    public QueryCondition nLike(T value) {
        return new QueryCondition(columnName, Operator.NOT_LIKE, tableDef, value);
    }

    /**
     * 左模糊查询
     */
    public QueryCondition likeL(T value) {
        return new QueryCondition(columnName, Operator.LIKE_LEFT, tableDef, value);
    }

    /**
     * 非左模糊查询
     */
    public QueryCondition nLikeL(T value) {
        return new QueryCondition(columnName, Operator.NOT_LIKE_LEFT, tableDef, value);
    }

    /**
     * 右模糊查询
     */
    public QueryCondition likeR(T value) {
        return new QueryCondition(columnName, Operator.LIKE_RIGHT, tableDef, value);
    }

    /**
     * 非左模糊查询
     */
    public QueryCondition nLikeR(T value) {
        return new QueryCondition(columnName, Operator.NOT_LIKE_RIGHT, tableDef, value);
    }

    /**
     * 范围[?, ?]*
     */
    public QueryCondition between(T value1, T value2) {
        return new QueryCondition(columnName, isDate ? Operator.DATE_BETWEEN : Operator.BETWEEN, tableDef, value1, value2);
    }

    /**
     * 不在范围
     */
    public QueryCondition nBetween(T value1, T value2) {
        return new QueryCondition(columnName, isDate ? Operator.DATE_NOT_BETWEEN : Operator.NOT_BETWEEN, tableDef, value1, value2);
    }

    /**
     * 集合
     */
    public QueryCondition in(T... values) {
        return new QueryCondition(columnName, Operator.IN, tableDef, values);
    }

    public QueryCondition in(QueryCondition queryCondition) {
        return new QueryCondition(columnName, Operator.IN, tableDef, queryCondition);
    }

    /**
     * 不在集合
     */
    public QueryCondition notIn(T... values) {
        return new QueryCondition(columnName, Operator.NOT_IN, tableDef, values);
    }

    /**
     * 集合
     */
    public QueryCondition in(List<T> list) {
        return new QueryCondition(columnName, Operator.IN, tableDef, list.toArray());
    }

    /**
     * 不在集合
     */
    public QueryCondition notIn(List<T> list) {
        return new QueryCondition(columnName, Operator.NOT_IN, tableDef, list.toArray());
    }

    /**
     * 等于 =
     */
    public QueryCondition eq(Column value) {
        return new QueryCondition(columnName, isDate ? Operator.DATE_EQUAL : Operator.EQUAL, tableDef, value);
    }

    /**
     * 不等于 !=
     */
    public QueryCondition ne(Column value) {
        return new QueryCondition(columnName, isDate ? Operator.DATE_NOT_EQUAL : Operator.NOT_EQUAL, tableDef, value);
    }

    /**
     * 大于 >
     */
    public QueryCondition gt(Column value) {
        return new QueryCondition(columnName, isDate ? Operator.DATE_GREATER_THAN : Operator.GREATER_THAN, tableDef, value);
    }

    /**
     * 大于等于 >=
     */
    public QueryCondition ge(Column value) {
        return new QueryCondition(columnName, isDate ? Operator.DATE_GREATER_THAN_OR_EQUAL : Operator.GREATER_THAN_OR_EQUAL, tableDef, value);
    }

    /**
     * 小于 <
     */
    public QueryCondition lt(Column value) {
        return new QueryCondition(columnName, isDate ? Operator.DATE_LESS_THAN : Operator.LESS_THAN, tableDef, value);
    }

    /**
     * 小于等于 <=
     */
    public QueryCondition le(Column value) {
        return new QueryCondition(columnName, isDate ? Operator.DATE_LESS_THAN_OR_EQUAL : Operator.LESS_THAN_OR_EQUAL, tableDef, value);
    }

    /**
     * 全模糊查询 like %?%
     */
    public QueryCondition like(Column value) {
        return new QueryCondition(columnName, Operator.LIKE, tableDef, value);
    }

    /**
     * 非全模糊查询 not like %?%
     */
    public QueryCondition nLike(Column value) {
        return new QueryCondition(columnName, Operator.NOT_LIKE, tableDef, value);
    }

    /**
     * 左模糊查询
     */
    public QueryCondition likeL(Column value) {
        return new QueryCondition(columnName, Operator.LIKE_LEFT, tableDef, value);
    }

    /**
     * 非左模糊查询
     */
    public QueryCondition nLikeL(Column value) {
        return new QueryCondition(columnName, Operator.NOT_LIKE_LEFT, tableDef, value);
    }

    /**
     * 右模糊查询
     */
    public QueryCondition likeR(Column value) {
        return new QueryCondition(columnName, Operator.LIKE_RIGHT, tableDef, value);
    }

    /**
     * 非左模糊查询
     */
    public QueryCondition nLikeR(Column value) {
        return new QueryCondition(columnName, Operator.NOT_LIKE_RIGHT, tableDef, value);
    }

    /**
     * 为null
     */
    public QueryCondition isNull() {
        return new QueryCondition(columnName, Operator.IS_NULL, tableDef);
    }

    /**
     * 不为null
     */
    public QueryCondition isNotNull() {
        return new QueryCondition(columnName, Operator.IS_NOT_NULL, tableDef);
    }

    public ColumnCondition asc() {
        return new ColumnCondition(columnName, QwOperator.ASC, tableDef);
    }

    public ColumnCondition desc() {
        return new ColumnCondition(columnName, QwOperator.DESC, tableDef);
    }


    public ColumnCondition x(Number value) {
        return new ColumnCondition(columnName, QwOperator.MUL, tableDef, value);
    }

    public ColumnCondition mul(Number value) {
        return new ColumnCondition(columnName, QwOperator.MUL, tableDef, value);
    }

    public ColumnCondition add(Number value) {
        return new ColumnCondition(columnName, QwOperator.ADD, tableDef, value);
    }

    public ColumnCondition sub(Number value) {
        return new ColumnCondition(columnName, QwOperator.SUB, tableDef, value);
    }

    public ColumnCondition div(Number value) {
        return new ColumnCondition(columnName, QwOperator.DIV, tableDef, value);
    }

    /**
     * 乘
     *
     * @param column
     * @return
     */
    public ColumnCondition mul(Column column) {
        return new ColumnCondition(columnName, QwOperator.MUL, tableDef, column);
    }


    /**
     * 乘
     *
     * @param column
     * @return
     */
    public ColumnCondition x(Column column) {
        return new ColumnCondition(columnName, QwOperator.MUL, tableDef, column);
    }

    /**
     * 加
     *
     * @param column
     * @return
     */
    public ColumnCondition add(Column column) {
        return new ColumnCondition(columnName, QwOperator.ADD, tableDef, column);
    }

    /**
     * 减
     *
     * @param column
     * @return
     */
    public ColumnCondition sub(Column column) {
        return new ColumnCondition(columnName, QwOperator.SUB, tableDef, column);
    }

    /**
     * 除
     *
     * @param column
     * @return
     */
    public ColumnCondition div(Column column) {
        return new ColumnCondition(columnName, QwOperator.DIV, tableDef, column);
    }

    public Column as(String as) {
        this.columnAs = as;
        return this;
    }


    @Override
    public String build(QueryWrapper qw) {
        return tableColumnName(qw) + (columnAs == null ? "" : " as " + columnAs);
    }

    public ColumnCondition tAs(String tableAs) {
        return new ColumnCondition(this, tableAs);
    }
}
